package com.bachelor.community.mapper;

import com.bachelor.community.model.entity.Question;
import com.bachelor.community.model.vo.QuestionVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface QuestionMapper extends BaseMapper<Question> {
    @Select("SELECT question.*, question.is_essential essential, user.username, user.id questioner FROM question left join user on question.creator=user.id ${ew.customSqlSegment}")
    IPage<QuestionVO> listQuestionVO(IPage<QuestionVO> page, @Param("ew") Wrapper<QuestionVO> wrapper);

    @Select("select question.*, question.is_essential essential, question.creator questioner, user.username, user.avatar, user.introduction, user.credit from question left join user on question.creator=user.id where question.id=#{id}")
    QuestionVO getQuestionVO(Long customId);

    @Select("SELECT question.title, question.id, question.is_essential essential, answer.*, user.username FROM question left join user on question.creator=user.id left join answer on answer.question_id=question.id " +
            "WHERE question.title like CONCAT('%', #{keyword}) or question.title like CONCAT(#{keyword}, '%')" +
            "or question.title like CONCAT('%', #{keyword}, '%') or question.title=#{keyword} or answer.content like CONCAT('%', #{keyword}, '%') ${ew.customSqlSegment}")
    IPage<QuestionVO> listQuestionsByKeyword(IPage<QuestionVO> page, @Param("ew") Wrapper<QuestionVO> wrapper, String keyword);
}
